import { useUserStore, useRouterStore } from '@/store'

/*
  单点登录
*/
export const useSSOLogin = () => {
  // `location.search` 得到url中?之后#之前的部分
  const searchParams = new URLSearchParams(location.search)
  const token = searchParams.get('token')

  if (!token) return

  const userStore = useUserStore()
  const routerStore = useRouterStore()

  routerStore.setState({
    loading: true,
    closeLoading: false
  })

  userStore
    .ssoLogin({
      token
    })
    .then(() => {
      routerStore.setState({
        loading: false,
        closeLoading: true
      })
    })
}
